﻿using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace Iot.AlarmDispose.WPF;

public class RowIndexDataGrid : DataGrid
{
    private volatile bool _flag;

    protected override void OnLoadingRow(DataGridRowEventArgs e)
    {
        base.OnLoadingRow(e);
        DataGridRowIndexHelper.SetRowIndex(e.Row, e.Row.GetIndex() + 1);
    }

    protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
    {
        base.OnItemsChanged(e);
        if (_flag) return;
        _flag = true;
        Application.Current?.Dispatcher?.BeginInvoke(DispatcherPriority.ApplicationIdle, () =>
        {
            Items.Refresh();
            _flag = false;
        });
    }
}